{% extends 'layout/layout.html' %}
{% import 'libs/libs.html' as libs %}
{% import 'libs/const.html' as const %}

{% block title_nav %}

<li><a href="{{ url_for('sys.menu_list')}}">菜单列表</a></li>
{% endblock %}

{% block body %}
<div class="row">
    <div class="col-lg-12 col-md-12 col-xs-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">基本信息</div>
            <div class="ibox-content">
              <form class="form-horizontal" method="POST" action="{{ url_for('sys.menu_save') }}" enctype="multipart/form-data">
                <input type="hidden" name="menu_id" id="menu_id" value="{{ f.menu_id }}">
                {{ libs.select('菜单类型', 'menu_type', f.menu_type,
                     [('请选择……','-1'),
                     ('主菜单', '0'),
                     ('子菜单','1')]) }}
                <div id='main_menu' style="display: none;">
                {{ libs.dbselect('主菜单名称', 'main_menu_id', f.parent_id, main_menu_list, 'endpoint_name', 'menu_id') }}
                </div>
                {{ libs.input('端点名称', 'endpoint_name', f.endpoint_name, help="如：用户管理") }}
                <div id='icon' style="display: none;">
                {{ libs.input('端点icon', 'endpoint_icon', f.endpoint_icon, help="如: fa fa-users") }}
                </div>
                {{ libs.input('端点', 'endpoint', f.endpoint, help="如: /user/index") }}
                {{ libs.input('排序', 'sort_order', f.sort_order|default(0), type='number', required='min=0 step=1', help="排序: 数值越大排序越靠前") }}
                <div class="form-group">
                    <label class="col-lg-4 control-label" id="label-{{ name }}">{{ label }}</label>
                    <div class="col-lg-8 {{error_class}}" id="control-{{ name }}">
                      <a href="javascript:;" onclick="javascript:menu_submit();" type="submit" id="submit" class="btn btn-primary">提交</a>
                    </div>
                </div>
              </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block footer %}
<script type="text/javascript">
$(document).ready(function() {
  var menu_type = $('#menu_type').val();
  var menu_id = $('#menu_id').val();
  if (menu_type == 0){
    $('#icon').show();
  }
  if (menu_id > 0 && menu_type == 1){
    $('#main_menu').show();
  }
  $('#menu_type').change(function(){
    var menu_type = $('#menu_type').val();
    if (menu_type == 0){
      $('#icon').show();
      $('#main_menu').hide();
      $('input[id=endpoint_name]').siblings('span').eq(0).html('如：用户管理');
    }else{
      $('#icon').hide();
      $('#main_menu').show();
      $('input[id=endpoint_name]').siblings('span').eq(0).html('如：用户列表');
    }
  });
});
function menu_submit(adom) {
  var menu_id = $('#menu_id').val();
  var endpoint = $('#endpoint').val();
  var endpoint_name = $('#endpoint_name').val();
  var endpoint_icon = $('#endpoint_icon').val();
  var menu_type     = $('#menu_type').val();
  var sort_order    = $('#sort_order').val();
  var main_menu_id  = $('#main_menu_id').val();
  if (menu_type == -1){
      if (endpoint_icon == ''){
        window.wxc.xcConfirm('请选择菜单类型', window.wxc.xcConfirm.typeEnum.info);
        return false;
      }
  }
  if (endpoint_name == ''){
      window.wxc.xcConfirm('端点名称不能为空', window.wxc.xcConfirm.typeEnum.info);
      return false;
  }
  if (menu_type == 1){
    if (main_menu_id <= 0){
      window.wxc.xcConfirm('请选择主菜单名称', window.wxc.xcConfirm.typeEnum.info);
      return false;
    }
  }
  if (menu_type == 0){
      if (endpoint_icon == ''){
        window.wxc.xcConfirm('端点icon不能为空', window.wxc.xcConfirm.typeEnum.info);
        return false;
      }
  }
  if (endpoint == ''){
      window.wxc.xcConfirm('端点不能为空', window.wxc.xcConfirm.typeEnum.info);
      return false;
  }
  if (sort_order == ''){
      window.wxc.xcConfirm('排序不能为空', window.wxc.xcConfirm.typeEnum.info);
      return false;
  }
  if (menu_id > 0){
    msg = '修改成功'
  }else{
    msg = '新增菜单成功'
  }
  $.get('/sys/menu_save', {'menu_id':menu_id, 'endpoint':endpoint, 'endpoint_name':endpoint_name, 'endpoint_icon':endpoint_icon, 'menu_type':menu_type, 'sort_order':sort_order, 'main_menu_id':main_menu_id}, function(res){
      if (res == 'ok') {
          window.wxc.xcConfirm(msg, window.wxc.xcConfirm.typeEnum.success);
          var t=setTimeout("window.location.href = '/sys/menu_list'",500)
      }else{
          window.wxc.xcConfirm(res, window.wxc.xcConfirm.typeEnum.info);
          return false;
      }
  });
}
</script>
{% endblock %}
